var DRApp = angular.module('DRApp',[]);
DRApp.controller('DRCtr',  function($scope,$http,$filter, filterFilter){
	   

	  $http.get("http://localhost:8080/DRMS/api/demandrequest/getAllDrmsDemandRequest").then(function (response) {
       // $scope.DRs = response.data;
       	$scope.data = response.data;
       		$scope.$watch('DRSearch',function(newVal,oldVal){
		
				//$scope.DRs = $filter('DRSearch')($scope.data);
				
				$scope.DRs = filterFilter($scope.data,newVal);
				console.log($scope.DRs);
			
       			$scope.pageSize = 8;
      	 		$scope.pages = Math.ceil($scope.DRs.length / $scope.pageSize);
       			$scope.newPages = $scope.pages > 5 ? 5 : $scope.pages;
       			$scope.pageList = [];
				$scope.selPage = 1;
       			$scope.setData = function () {
					$scope.items = $scope.DRs.slice(($scope.pageSize * ($scope.selPage - 1)), ($scope.selPage * $scope.pageSize));
			}	
     				$scope.items = $scope.DRs.slice(0, $scope.pageSize);
     			 for (var i = 0; i < $scope.newPages; i++) {
						$scope.pageList.push(i + 1);
			} 
				$scope.selectPage     = function (page) {
					if (page < 1 || page > $scope.pages) return;
			
					if(page == 1){
						var newpageList = [1,2,3,4,5];
						$scope.pageList = newpageList;
				}
					if (page > 2) {
						var newpageList       = [];
						for (var i            = (page - 3) ; i < ((page + 2) > $scope.pages ? $scope.pages : (page + 2)) ; i++) {
						newpageList.push(i + 1);
					}
						$scope.pageList       = newpageList;
					}
						$scope.selPage        = page;
						$scope.setData();
						$scope.isActivePage(page);
					};
					
					$scope.isActivePage   = function (page) {
						return $scope.selPage == page;
					};
					$scope.Previous       = function () {
						$scope.selectPage($scope.selPage - 1);
					}
					$scope.Next           = function () {
						$scope.selectPage($scope.selPage + 1);
					};
	
			
		});

    })
	   
});
 